#include "Wall.h"
#include "TextureManager.h"
#include "InputHandler.h"
#include "Defines.h"
#include "Game.h"


Wall::Wall(const LoadedParams* Params, int animationSpeed, int numFrames, int health, Player* player) : Tile(Params, animationSpeed, numFrames), maxHealth(health), mPlayer(player)
{
	mUpgradeButton = new GenericButton(new LoadedParams(position.getX(), position.getY() - 50, 32, 32, "upgrade"));
	mSellButton = new GenericButton(new LoadedParams(position.getX(), position.getY() + 50, 32, 32, "sell"));
	state = 0;
	curHealth = maxHealth;
}


void Wall::Draw()
{
	if(isSelected())
	{
		mUpgradeButton->Draw();
		mSellButton->Draw();
	}
	DisplayHealthBar();
	Tile::Draw();
}

void Wall::Update()
{
	if(isSelected())
	{
		mUpgradeButton->Update();
		mSellButton->Update();
		if (mUpgradeButton->isClicked())
		{
			if (mPlayer->PurchaseUpgrade(10, 20, 0))
				Upgrade(1);
		}
		else if (mSellButton->isClicked())
		{
			mPlayer->addResources(10, 0, 0);
			active = false;
		}
			
		else if(InputHandler::Instance()->Clicked())
			selected = false;
	}
	Tile::Update();
}

void Wall::Shutdown()
{
	Tile::Shutdown();
}

void Wall::Upgrade(short wallType)
{
	switch(wallType)
	{
	case 0:
		textureID = "wall";
		maxHealth = 3;
		setHealth(3);
		break;
	case 1:
		textureID = "stonewall";
		maxHealth = 10;
		setHealth(8);
		break;
	default:
		printf("No correct Wall Type Specified");
		break;
	}
}

void Wall::takeDamage(int dmg)
{
	curHealth -= dmg;
}

void Wall::onMouseOver()
{
	
}

bool Wall::isActive()
{
	return Tile::isActive();
}

void Wall::DisplayHealthBar()
{
	if (isMouseOver() && !selected)
	{
		TextureManager::Instance()->Draw("health_bg", position.getX(), position.getY() - 12, 32, 10, Game::Instance()->getRenderer());
		int x = 30 * (curHealth / (float)maxHealth);
		TextureManager::Instance()->Draw("health_bar", position.getX() + 1, position.getY() - 11, x, 9, Game::Instance()->getRenderer());
	}
}

void Wall::setHealth(int newHealth)
{
	if (newHealth > maxHealth)
		curHealth = maxHealth;
	else
		curHealth = newHealth;
}